package com.ctbu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ctbu.api.BankService;
import com.ctbu.entity.BankInfo;
import com.ctbu.entity.LoanProduct;
import com.ctbu.entity.LoanProductType;
import com.ctbu.mapper.LoanProductMapper;
import com.ctbu.mapper.LoanProductTypeMapper;
import com.ctbu.service.LoanProductService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctbu.tools.ResponseResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * <p>
 * 贷款产品表 服务实现类
 * </p>
 *
 * @author lli
 * @since 2024年06月18日
 */
@Service
public class LoanProductServiceImpl extends ServiceImpl<LoanProductMapper, LoanProduct> implements LoanProductService {
    @Autowired
    private LoanProductMapper loanProductMapper;

    @Autowired
    private LoanProductTypeMapper loanProductTypeMapper;
    /*@Autowired
    private RestTemplate restTemplate;*/

    @Autowired
    private BankService bankService;


    @Override
    public ResponseResult<Object> selectList(int pageNum, int pageSize, String productName, String interestRate){

        //开启分页
        PageHelper.startPage(pageNum,pageSize);
        //条件查询
        LambdaQueryWrapper<LoanProduct> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        if(!productName.equals("")){
            lambdaQueryWrapper.like(LoanProduct::getProductName,productName);
        }
        if(!interestRate.equals("")){
            lambdaQueryWrapper.eq(LoanProduct::getInterestRate,interestRate);
        }
        List<LoanProduct> list = loanProductMapper.selectList(lambdaQueryWrapper);

        list.forEach(loanProduct -> {
            //处理类型的数据
            LoanProductType loanProductType=loanProductTypeMapper.selectById(loanProduct.getLoanTypeId());
            //将类型信息封装到产品对象中
            loanProduct.setLoanProductType(loanProductType);
            //处理银行数据
            /*BankInfo bankInfo = restTemplate.getForObject("http://bank-service/bankInfo/remote/"
                    + loanProduct.getBankId(), BankInfo.class);*/

            BankInfo bankInfo= bankService.byId(loanProduct.getBankId());
            loanProduct.setBankInfo(bankInfo);
        });

        //将查询结果封装到对应分页中
        PageInfo<LoanProduct> pageInfo = new PageInfo<>(list);
        return ResponseResult.success(pageInfo,"查询成功");

    }

}
